//
//  ActListGViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/10/6.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//

#import "MActViewController.h"
#import "HAODEsingleton.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "JSONKit.h"
#import "addhuiyuanViewController.h"
#import "ActCell.h"
#import "RDVTabBarController.h"
#import "MJRefresh.h"
#import "UIButton+WebCache.h"
#import "huiyuanDetailsViewController.h"
#import "GetProductListViewController.h"
#import "TheLoginViewController.h"
#import "shouyingViewController.h"
#import "BusinessActDetailViewController.h"
@interface MActViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UISearchBarDelegate>
@property (strong, nonatomic) NSString *areaValue;
@property (strong, nonatomic) NSString*cityValue;

@end

@implementation MActViewController

{
    UISearchBar *search;
    HAODEsingleton* commdity;
    MJRefreshHeaderView* _header;
    MJRefreshFooterView* _footer;
    
    NSMutableArray*joinTime;
    NSMutableArray*ActendDate;
    NSMutableArray*actId;
    NSMutableArray*ActbeginDate;
    NSMutableArray*actName;
    NSMutableArray*isUsedName;
    NSMutableArray*actType;
    NSMutableArray*actuseTime;
    NSMutableArray*actStatus;
   
    NSMutableArray*couponId;
    NSMutableArray*title;
    NSMutableArray*price;
    NSMutableArray*qty;
    NSMutableArray*beginDate;
    NSMutableArray*endDate;
    NSMutableArray*imageUrl;
    
    NSMutableArray*imageUrlFull;
    NSString *result;
    int pageNumber;
    MBProgressHUD *progressHud;
    UILabel*noLabel;
    
}
@synthesize alt = alt;

-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    
  
    self.navigationController.navigationBarHidden=NO;
        self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    
    self.title=@"促销活动";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];

    
    [self add];
    
}


- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
    [self addFooter];
    _table.delegate=self;
    _table.dataSource=self;
    [self.view addSubview:_table];
    _table.separatorStyle=UITableViewCellSeparatorStyleNone;
    commdity=[HAODEsingleton shared];
    
    
}


-(void)add{
    
    [noLabel removeFromSuperview];
    
    self.sat=nil;
    
    
    pageNumber = 1;
    
    
    NSUserDefaults *user12 = [NSUserDefaults standardUserDefaults];
    NSString* Token12 = [user12 objectForKey:@"tokenId"];
    
    NSMutableDictionary *param12 = [NSMutableDictionary new];
    [param12 setValue:Token12 forKey:@"tokenId"];
    [param12 setValue:commdity.memberId forKey:@"memberId"];
    
    
    NSLog(@"====%@",param12);
    
    [HttpRequest requestData:@"Business/MAct/List" params:param12 success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
            NSLog(@"啊啊啊啊啊====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                NSString*TotalCount=[severDataDict.dataDict objectForKey:@"TotalCount"];
                
                if (TotalCount.intValue==0) {
                    
                    noLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,ScreenHeight/2-50, ScreenWidth, 17)];
                    noLabel.text=@"无数据";
                    noLabel.textColor=[UIColor blackColor];
                    noLabel.textAlignment=NSTextAlignmentCenter;
                    
                    [self.view addSubview:noLabel];
                }else{

                NSMutableArray* arrr=[[NSMutableArray alloc]init];
                
                arrr=[severDataDict.dataDict objectForKey:@"DataLine"];
                endDate= [[NSMutableArray alloc] init];
                actId= [[NSMutableArray alloc] init];
                beginDate= [[NSMutableArray alloc] init];
                ActendDate= [[NSMutableArray alloc] init];
                actName= [[NSMutableArray alloc] init];
                price= [[NSMutableArray alloc] init];
                ActbeginDate= [[NSMutableArray alloc] init];
                actuseTime= [[NSMutableArray alloc] init];
                actStatus= [[NSMutableArray alloc] init];
                actType= [[NSMutableArray alloc] init];
                joinTime= [[NSMutableArray alloc] init];
                    imageUrlFull= [[NSMutableArray alloc] init];
                if (arrr.count>0) {
                    
                    
                    for (NSDictionary* dicc in arrr )
                    {
                        
                        
                         [imageUrlFull addObject:[dicc valueForKey:@"imageUrlFull"]];
                        [joinTime addObject:[dicc valueForKey:@"joinTime"]];
                        [ActendDate addObject:[dicc valueForKey:@"endDate"]];
                        [actId addObject:[dicc valueForKey:@"actId"]];
                        [ActbeginDate addObject:[dicc valueForKey:@"beginDate"]];
                        [actName addObject:[dicc valueForKey:@"actName"]];
                        [isUsedName addObject:[dicc valueForKey:@"isUsedName"]];
                        [actType addObject:[dicc valueForKey:@"actType"]];
                        [actStatus addObject:[dicc valueForKey:@"actStatus"]];
                        [actuseTime addObject:[dicc valueForKey:@"joinTime"]];
                    }
                    
                    
                }
              [_table reloadData];
                }
            }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                // [Global showTextHud:requestResultStr view:self.view];
                [Global showTextHudSub:requestResultStr view:self.view];
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];


    
    
    
    
    
    
}





//上拉刷新
- (void)addFooter
{
    
    
    
    MJRefreshFooterView* footer = [MJRefreshFooterView footer];
    footer.scrollView = _table;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView* refreshView)
    {
        
        
        
        [self performSelector:@selector(doneWithView:) withObject:refreshView afterDelay:3.0f];
        NSLog(@"%@--->正在刷新", refreshView.class);
    };
    
}
- (void)doneWithView:(MJRefreshBaseView *)refreshView
{
    [_table reloadData];
    [refreshView endRefreshing];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [actName count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    ActCell* cell = (ActCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"ActCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    UIButton*jiantou = [[UIButton alloc]init];
    jiantou.frame = CGRectMake(0,0, 8, 13);
    [jiantou setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
    
    //cell.accessoryView = jiantou;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIView*bianxian=[[UIView alloc]initWithFrame:CGRectMake(8, 8, ScreenWidth-16,200-8)];
    
    [bianxian.layer setCornerRadius:0.5];
    [bianxian.layer setBorderWidth:0.5];
    [bianxian.layer setMasksToBounds:YES];
    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){204.0/255.0, 204.0/255.0, 204.0/255.0, 1.0  });
    [bianxian.layer setBorderColor:colorref2];//边框颜色
    [cell.contentView addSubview:bianxian];
    
    [cell.imageUrlFull.layer setBorderColor:colorref2];//边框颜色
    [cell.imageUrlFull.layer setCornerRadius:0.5];
    [cell.imageUrlFull.layer setBorderWidth:0.1];
    [cell.imageUrlFull.layer setMasksToBounds:YES];
    CGColorSpaceRef colorSpace21 = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref21 = CGColorCreate(colorSpace21,(CGFloat[]){ 0, 0, 0, 0.3 });
    [cell.imageUrlFull.layer setBorderColor:colorref21];//边框颜色
    [cell.imageUrlFull setImageWithURL:[NSURL URLWithString:[imageUrlFull objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@""]];
    [cell.imageUrlFull setContentMode:UIViewContentModeScaleToFill];
    
    
    cell.actName.text =[NSString stringWithFormat:@"%@",  [actName objectAtIndex:indexPath.row]];
    cell.actName.font=[UIFont systemFontOfSize:15];
    cell.actName.textColor=[UIColor blackColor];
    
    
    
    cell.time.textColor=hongsecolor;
    cell.time.text = [NSString stringWithFormat:@"参加时间 %@",[joinTime objectAtIndex:indexPath.row]];
    
    NSMutableAttributedString *AttributedStr2 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"参加时间%@",[joinTime objectAtIndex:indexPath.row]]];
    
    [AttributedStr2 addAttribute:NSFontAttributeName
     
                           value:[UIFont systemFontOfSize:14.0]
     
                           range:NSMakeRange(0, 4)];
    
    [AttributedStr2 addAttribute:NSForegroundColorAttributeName
     
                           value:zhonghuiColor
     
                           range:NSMakeRange(0, 4)];
    
    
    cell.time.attributedText = AttributedStr2;

    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return  200;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    commdity.orderId=actId[indexPath.row];
    
    BusinessActDetailViewController*vc1=[[BusinessActDetailViewController alloc] init];
    
    [self.navigationController pushViewController:vc1 animated:YES];
}



- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}


@end
